Skip to main content

Modules and Contexts

Modules

A Module is a place for giving things names. In particular, it can declare actions, lists and captures, scopes, tags, modes, settings and applications. In Python, you can construct a module like so:

from talon import Module
mod = Module()

All Actions, Lists etc. must first be declared via a Module before they can be referenced in .talon or .py files. See the concept sections linked above for how to do this in each case.

Contexts

A context specifies conditions under which to add new behavior or override existing behavior. A context can check for several properties like your OS, the name of the current application, etc. Within a particular context you can implement/override the behavior of actions, adjust settings, activate tags, and redefine lists. Note that you cannot define new voice commands in Python, that can only be done in .talon files.

In Python, you can construct a context like so:

from talon import Context
ctx = Context()

When initially constructed, a context has no conditions attached, and so it is always active. You can make this context conditional by setting the matches property:

ctx.matches = """
app: notepad_app
os: windows
"""

Multiple contexts can be active at any one time, we might have the one mentioned above as well as one with ctx.matches = "os: windows". Since contexts can override behavior, Talon has a set of heuristics to work out which context should 'win' in the event that two or more override the same behavior. A useful approximation of these heuristics is that contexts with more matching rules will win. This concept can be used to make sure your overrides are used in preference to those implemented elsewhere (e.g. in Talon Community). For example if we wanted a more specific matcher than the one above we might add in a language: en requirement:

from talon import Context
more_specific_ctx = Context()

more_specific_ctx.matches = """
app: notepad_app
os: windows
language: en
"""

See examples in the Actions, lists, and Tags sections for information about using Contexts with those features.